//
//  NewsViewController.m
//  MyMovie
//
//  Created by apple on 16/5/10.
//  Copyright © 2016年 ljp. All rights reserved.
//

#import "NewsViewController.h"
#import "StarView.h"
#import "JSONDataService.h"
#import "News.h"
#import "NewsCell.h"

#import <MediaPlayer/MediaPlayer.h>

@interface NewsViewController ()<UITableViewDelegate, UITableViewDataSource>
{

    NSMutableArray *dataArray;
}

@property (strong, nonatomic) IBOutlet UITableView *newsTableView;
@end

@implementation NewsViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    [self loadJSONData];

}

-(void)loadJSONData {


    NSArray *result = [JSONDataService loadJSONFielWithName:@"news_list.json"];
    dataArray = [[NSMutableArray alloc] init];


    for (NSDictionary *dic in result) {

        News *news = [[News alloc] initWithDictionary:dic];
        [dataArray addObject:news];
    }


}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return dataArray.count;


}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    UITableViewCell *cell = nil;
    if (indexPath.row == 0) {

        cell = [tableView dequeueReusableCellWithIdentifier:@"TopNewsCell"];

        News *news = [dataArray firstObject];

        UIImageView *topImageView = [cell viewWithTag:12345];
        UILabel *topLable = [cell viewWithTag:54321];

        NSURL *url = [NSURL URLWithString:news.imageName];
        [topImageView sd_setImageWithURL:url];
        topLable.text = news.title;

    }else {

        cell = [tableView dequeueReusableCellWithIdentifier:@"NewsCell"];
        NewsCell *newsCell = (NewsCell *)cell;
        [newsCell setNews:dataArray[indexPath.row]];
    }

       return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        return 180;
    }
    return 80;
}
#pragma mark 父协议
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    if (scrollView.contentOffset.y < -64) {

        UITableViewCell *cell = [_newsTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
        UIImageView *imageView = [cell.contentView viewWithTag:12345];

        //放大倍数
        //放大倍数 = 放大后的高度/图片原高度
        //放大后的高度 = 图片原高度 + 偏移量

        CGFloat scale = (180 + (-64 - scrollView.contentOffset.y))/180;

        //使用transform来放大视图
        imageView.transform = CGAffineTransformMakeScale(scale, scale);

        //防止图片被裁剪
        cell.clipsToBounds = NO;
        cell.contentView.clipsToBounds = NO;
        //设置图片顶部在导航栏下方
        imageView.top = scrollView.contentOffset.y + 64;

    }else {

    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    News *news = dataArray[indexPath.row];
    //判断新闻类型
    switch (news.type) {
        case NewsTypeNone:
        {
          UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"WebNewsVC"];
            //进行push操作的视图控制器设置hidesBottomBarWhenPushed
            vc.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case NewsTypeImage:
        {
            
            UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"ImageListVC"];
            //进行push操作的视图控制器设置hidesBottomBarWhenPushed
            vc.hidesBottomBarWhenPushed = YES;

            [self.navigationController pushViewController:vc animated:YES];

            break;
        }
        case NewsTypeVideo:
        {
//            UIViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VideoViewController"];
//            vc.hidesBottomBarWhenPushed = YES;
//            [self.navigationController pushViewController:vc animated:YES];
            //http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4
            NSURL *url = [NSURL URLWithString:@"http://vf1.mtime.cn/Video/2012/04/23/mp4/120423212602431929.mp4"];
            MPMoviePlayerViewController *moviePlayerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
            [self presentViewController:moviePlayerVC animated:YES completion:nil];

            break;
        }
        default:
            break;
    }


}

@end
